package org.ttitfly.workflow.engine.basic;

import java.util.List;

import org.ttitfly.workflow.Session;
import org.ttitfly.workflow.SessionHolder;
import org.ttitfly.workflow.engine.Action;
import org.ttitfly.workflow.engine.Process;
import org.ttitfly.workflow.engine.run.Task;
import org.ttitfly.workflow.handler.Handler;


/**
 * 子流程活动，运行时会启动一个子流程，子流程结束时会调用{@link #execute()}方法来完成本活动
 * 
 * @author 潘海春
 * @version 2009-9-18
 */
public class SubProcessActivity extends BasicActivity {

	/**
	 * 子流程名称
	 */
	private String subProcessName;

	/**
	 * 启动action
	 */
	private String startAction;

	/**
	 * 构造函数
	 * 
	 * @param parent
	 * @param name
	 * @param subProcessName
	 * @param startAction
	 * @param actions
	 * @param activeHandlers
	 * @param closeHandlers
	 */
	public SubProcessActivity(Process parent, String name,
			String subProcessName, String startAction, Action defaultAction,
			List<Handler> activeHandlers, List<Handler> closeHandlers) {
		super(parent, name, defaultAction, null, null, null, activeHandlers, closeHandlers);
		if (subProcessName == null || "".equals(subProcessName)) {
			throw new IllegalArgumentException("参数[subProcessName]不能为空！");
		}
		this.subProcessName = subProcessName;
		if (startAction != null && !"".equals(startAction)) {
			this.startAction = startAction;
		}
	}

	@Override
	protected void onActive() throws Exception {
		super.onActive();
		Task task = SessionHolder.currentSession().currentTask();
		Session session = SessionHolder.currentSession().fork();
		if (startAction == null) {
			session.start(task, subProcessName);
		} else {
			session.start(task, subProcessName, startAction);
		}
		session.close();
	}
}
